#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const int maxn = 2e4;
ll a[maxn];
ll t, n;
struct node1
{
    ll y, next_s;
} e[maxn << 1];
ll head[maxn], cnt;
ll hp[maxn];
void init()
{
    memset(head, -1, sizeof(head));
    cnt = 0;
    return;
}
void add(ll x, ll y)
{
    e[cnt].y = y;
    e[cnt].next_s = head[x];
    head[x] = cnt++;
    return;
}
ll ans[maxn];
void dfs(ll u)
{
    ll sum = 2 * hp[u];
    for (ll i = head[u]; i != -1; i = e[i].next_s)
    {
        ll v = e[i].y;
        dfs(v);
        sum += hp[v];
    }
    if (u == 1 || head[u] == -1)
        a[u] = sum - hp[u];
    else
        a[u] = sum;
    ans[0] += (sum - hp[u]);
    return;
}
ll sum1;
void dfs1(ll u, ll f, ll k)
{
    ll sum = 2 * hp[u];
    // if (u == 1)
    // {
    //     sum = hp[u];
    // }
    ll cnt = 0;
    for (ll i = head[u]; i != -1; i = e[i].next_s)
    {
        ll v = e[i].y;
        dfs1(v, u, k);
        if (hp[v])
            cnt++;
        sum += hp[v];
    }
    if (hp[u] == 0)
    {
        a[u] = 0;
        return;
    }
    else if (hp[f] == 0 || head[u] == -1 || cnt == 0)
        a[u] = sum - hp[u];
    else
        a[u] = sum;
    ans[k] += (sum - hp[u]);
    return;
}

int main()
{
    IOS;
    cin >> t;
    while (t--)
    {
        init();
        memset(ans, 0, sizeof(ans));
        cin >> n;
        ll x;
        for (ll y = 2; y <= n; y++)
        {
            cin >> x;
            add(x, y);
        }
        for (ll i = 1; i <= n; i++)
        {
            cin >> hp[i];
        }
        dfs(1);
        for (ll i = 1; i <= n; i++)
        {
            ll maxx = 0, id = 0;
            for (ll j = 1; j <= n; j++)
            {
                // cout << a[j] << " ";
                if (a[j] > maxx)
                {
                    maxx = a[j];
                    id = j;
                }
            }
            // cout << endl;
            // ans[i] = ans[i - 1] - maxx;
            hp[id] = 0;
            dfs1(1, maxn - 1, i);
            // a[id] = 0;
        }
        cout << ans[0];
        for (ll i = 1; i <= n; i++)
        {
            cout << " " << ans[i];
        }
        if (t)
        {
            cout << endl;
        }
    }

    return 0;
}
